﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaseUnits;

namespace Carbon_Tracker
{
    public partial class frmPlantTree : Form
    {
        public frmPlantTree()
        {
            InitializeComponent();
        }

        private void frmPlantTree_Load(object sender, EventArgs e)
        {
            loadGrids();
        }
        private void loadGrids()
        {
            try
            {
                this.treeView.Nodes.Clear();
                BaseUnits.Builders.PowerPlantBuilder pb = new BaseUnits.Builders.PowerPlantBuilder(new DatabaseInterface.Connection());
                List<BaseUnits.IPowerPlant> powerPlants = pb.getPowerPlants();
                TreeNode plantNode;
                TreeNode unitNode;
                foreach (BaseUnits.IPowerPlant plant in powerPlants)
                {
                    plantNode = new TreeNode(plant.Description);
                    plantNode.Tag = plant;
                    plantNode.ImageIndex = 0;
                    foreach (BaseUnits.IPowerUnit unit in plant.PowerUnits)
                    {
                        unitNode = new TreeNode(unit.Description);
                        unitNode.Tag = unit;
                        unitNode.ImageIndex = 1;
                        plantNode.Nodes.Add(unitNode);
                    }
                    this.treeView.Nodes.Add(plantNode);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void treeView_DoubleClick(object sender, EventArgs e)
        {
            TreeNode node = this.treeView.SelectedNode;
            if (node.Tag is BaseUnits.IPowerUnit)
            {
                frmPowerUnitManager f = new frmPowerUnitManager(new DatabaseInterface.Connection(), (IPowerUnit)node.Tag);
                f.ShowDialog();
                this.loadGrids();
            }
            if (node.Tag is BaseUnits.IPowerPlant)
            {
                MessageBox.Show("Power Plant");
            }
        }
    }
}
